package com.shi.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.shi.util.EncodeUtil;
import com.shi.util.TuPanel;

import cn.hutool.core.io.file.FileWriter;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.SwingConstants;
import java.awt.SystemColor;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LoginJFrame extends JFrame {

	private TuPanel contentPane; // 图片类 TuPanel - 背景图片工具类
	private JPasswordField passwordField;// 密码
	public static EncodeJFrame encodeJFrame;
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginJFrame frame = new LoginJFrame();
					// setVisible 设置窗口是否显示 默认为false
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginJFrame() {
	
		// setDefaultCloseOperation 设置关闭按钮 在点击关闭按钮的同时进行什么操作
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(455, 200, 608, 473);
		setLocationRelativeTo(null);//居中
//		ImageIcon img=new ImageIcon("/conImage.jpg");
		ImageIcon img=new ImageIcon(LoginJFrame.class.getResource("/com/shi/resources/conImage.jpg"));
		 
		setIconImage(img.getImage());

	
		// 背景图片
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (screen.getWidth() - this.getWidth()) / 2;
		int y = (int) (screen.getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);

		contentPane = new TuPanel();
		contentPane.tu  = LoginJFrame.class.getResource("/com/shi/resources/background.jpg");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		JLabel label_1 = new JLabel("密码：");
		label_1.setFont(new Font("华文楷体", Font.PLAIN, 22));
		label_1.setBounds(68, 180, 93, 38);
		contentPane.add(label_1);
		// 密码文本
		passwordField = new JPasswordField();
		passwordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				
				if((char)e.getKeyChar()==KeyEvent.VK_ENTER) {
				
					String password = passwordField.getText();
					String realpassword = null;
						
						try {
			
							
							Properties p = new Properties();
							// 把文件流加载进Properties类中
				
							p.load(LoginJFrame.class.getClassLoader().getResourceAsStream("com/shi/resources/application.properties"));
							 realpassword = p.getProperty("password");
							
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
						
						
						
						try {
					   	File f = new File(EncodeUtil.dataFilePath);
				    	  
				    	  if(!f.exists()){
				    		  System.out.println("创建文件夹");
				    		  f.mkdirs();
				    	  }
				     		 
				    	   f = new File(EncodeUtil.testFilename);
				    	   
				    	  if(!f.exists()){
				    		    System.out.println("创建文件");
				    		    FileWriter writer = new FileWriter(f);
					        	writer.write("权限校验");
				    		f.createNewFile();
				    	  	}else{
				    		FileWriter writer = new FileWriter(f);
				        	writer.write("权限校验");
				    	  	}
				    	  
				    	  
			
				        	
						} catch (Exception e4){
							
							JOptionPane.showMessageDialog(null, "无创建文件权限!");
							System.exit(1000);
						}
					  	 
					 
						if (password.equals(realpassword)){
							encodeJFrame = new EncodeJFrame();
							encodeJFrame.setVisible(true);
								setVisible(false);
						} else{

							JOptionPane.showMessageDialog(null, "密码错误");
						}
				}
					
				
			}
		});
		passwordField.setBounds(163, 180, 275, 38);
		contentPane.add(passwordField);



		JButton button = new JButton("确认");
		button.setFont(new Font("华文楷体", Font.PLAIN, 22));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String password = passwordField.getText();
				String realpassword = null;
					
					try {
		
						
						Properties p = new Properties();
						// 把文件流加载进Properties类中
			
						p.load(LoginJFrame.class.getClassLoader().getResourceAsStream("com/shi/resources/application.properties"));
						 realpassword = p.getProperty("password");
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					try {
					 	File f = new File(EncodeUtil.dataFilePath);
				    	  
				    	  if(!f.exists()){
				    		  System.out.println("创建文件夹");
				    		  f.mkdirs();
				    	  }
				     		 
				    	   f = new File(EncodeUtil.testFilename);
				    	   
				    	  if(!f.exists()){
				    		    System.out.println("创建文件");
				    		    FileWriter writer = new FileWriter(f);
					        	writer.write("权限校验");
				    		f.createNewFile();
				    	  	}else{
				    		
				    	  
				    		FileWriter writer = new FileWriter(f);
				        	writer.write("权限校验");
				    	  	}
				    	  
				    	  
				    	  
						} catch (Exception e4){
							
							JOptionPane.showMessageDialog(null, "无创建文件权限!");
							System.exit(1000);
						}
					
					
					if (password.equals(realpassword)){
						encodeJFrame = new EncodeJFrame();
						encodeJFrame.setVisible(true);
							setVisible(false);
					} else{

						JOptionPane.showMessageDialog(null, "密码错误");
					}
			}
				
		});
		button.setBounds(454, 180, 101, 38);
		contentPane.add(button);


	}


}
